package fhq.hcmute.edu.vn.activities;

import fhq.hcmute.edu.vn.network.ComputerInfos;
import fhq.hcmute.edu.vn.network.Network;
import fhq.hcmute.edu.vn.network.PhoneInfos;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ConnectManualActivity extends Activity {

	private Button btnOK;
	private Button btnCancel;
	private TextView txtIpAddress;
	private TextView txtPort;
	private TextView txtPassword;
	private String phoneIMEI;
	private String deviceModelNumber;
	private String deviceName;

	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_connect_manual);
		btnOK = (Button) findViewById(R.id.btnOK);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		txtIpAddress = (TextView) findViewById(R.id.txtIpAddress);
		txtPort = (TextView) findViewById(R.id.txtPort);
		txtPassword = (TextView) findViewById(R.id.txtPassword);
		btnCancel.setOnClickListener(onCancelClick);
		btnOK.setOnClickListener(onOKClick);
		String telephonyService = MainActivity.TELEPHONY_SERVICE;
		TelephonyManager telephonyManager =
				(TelephonyManager) getSystemService(telephonyService);
		phoneIMEI = telephonyManager.getDeviceId();
		deviceModelNumber = Build.MODEL;
		deviceName = Build.MANUFACTURER + " " + android.os.Build.PRODUCT;

	}

	private View.OnClickListener onOKClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			String password = txtPassword.getText().toString();
			String ipAddress = txtIpAddress.getText().toString();
			int port = Integer.parseInt(txtPort.getText().toString());
			PhoneInfos phoneInfos =
					new PhoneInfos(password, deviceName, deviceModelNumber,
							phoneIMEI);
			ComputerInfos computerInfos =
					new ComputerInfos("", ipAddress, port, password);
			Network network = Network.getInstance();
			network.setActivity(ConnectManualActivity.this);
			network.setHandler(ConnectionHandler
					.getInstance(ConnectManualActivity.this));
			network.execute(computerInfos, phoneInfos);
		}
	};
	private View.OnClickListener onCancelClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};
}
